package com.dbgs.insurance.insuredperson;

import android.content.Context;
import android.content.SharedPreferences;

/** The class is used for saving simple key-value in a xml file. */
public class XmlPerference {

    private Context context;
    /**
     * Preferences Name that we use.
     */
    public static final String Pref_Name = "sharedata";

    /**
     * Holds the single instance that is shared by the process.
     */
    private static XmlPerference sInstance = null;

    /**
     * Return the single SharedPreferences instance.
     */
    public static XmlPerference getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new XmlPerference(context);
        }
        return sInstance;
    }

    public XmlPerference(Context context) {
        this.context = context;
    }

    /**
     * Save the String-String key-values in sharePreference file.
     */
    public void saveKey(String mKey, String mValue) {
        SharedPreferences mSharePrefs = context.getSharedPreferences(Pref_Name, 0);
        SharedPreferences.Editor editor;
        if (mSharePrefs != null) {
            editor = mSharePrefs.edit();
            editor.putString(mKey, mValue);
            editor.commit();
        }
    }
    
    public void saveKey(String mKey, Long mValue) {
        SharedPreferences mSharePrefs = context.getSharedPreferences(Pref_Name, 0);
        SharedPreferences.Editor editor;
        if (mSharePrefs != null) {
            editor = mSharePrefs.edit();
            editor.putLong(mKey, mValue);
            editor.commit();
        }
    }
    

    public void saveKey(String mKey, String mValue, String prefName) {
        SharedPreferences mSharePrefs = context.getSharedPreferences(prefName, 0);
        SharedPreferences.Editor editor;
        if (mSharePrefs != null) {
            editor = mSharePrefs.edit();
            editor.putString(mKey, mValue);
            editor.commit();
        }
    }

    public String getKeyStringValue(String mKey, String mDefValue) {
        String mStr = null;
        SharedPreferences mSharePrefs = context.getSharedPreferences(Pref_Name, 0);
        if (mSharePrefs != null) {
            mStr = mSharePrefs.getString(mKey, mDefValue);
        }
        return mStr;
    }

    public String getKeyStringValue(String mKey, String mDefValue, String prefName) {
        String mStr = null;
        SharedPreferences mSharePrefs = context.getSharedPreferences(prefName, 0);
        if (mSharePrefs != null) {
            mStr = mSharePrefs.getString(mKey, mDefValue);
        }
        return mStr;
    }

    public int getKeyIntValue(String mKey, int mDefValue) {
        SharedPreferences mSharePrefs = context.getSharedPreferences(Pref_Name, 0);
        int mInt = 0;
        if (mSharePrefs != null) {
            mInt = mSharePrefs.getInt(mKey, mDefValue);
        }
        return mInt;
    }

    public boolean getKeyBooleanValue(String mKey, boolean mDefValue) {
        SharedPreferences mSharePrefs = context.getSharedPreferences(Pref_Name, 0);
        boolean mBool = false;
        if (mSharePrefs != null) {
            mBool = mSharePrefs.getBoolean(mKey, mDefValue);
        }
        return mBool;
    }

    public Float getKeyFloatValue(String mKey, int mDefValue) {
        SharedPreferences mSharePrefs = context.getSharedPreferences(Pref_Name, 0);
        Float mFloat = null;
        if (mSharePrefs != null) {
            mFloat = mSharePrefs.getFloat(mKey, mDefValue);
        }
        return mFloat;
    }

    public Long getKeyLongValue(String mKey, int mDefValue) {
        SharedPreferences mSharePrefs = context.getSharedPreferences(Pref_Name, 0);
        Long mlong = null;
        if (mSharePrefs != null) {
        	mlong = mSharePrefs.getLong(mKey, mDefValue);
        }
        return mlong;
    }
    /**
     * 保存整型的键值对到配置文件当中.
     */
    public void saveKey(String mKey, int mValue) {
        SharedPreferences mSharePrefs = context.getSharedPreferences(Pref_Name, 0);
        SharedPreferences.Editor editor;
        if (mSharePrefs != null) {
            editor = mSharePrefs.edit();
            editor.putInt(mKey, mValue);
            editor.commit();
        }
    }

    /**
     * 保存boolean类型的键值对到配置文件当中.
     */
    public void saveKey(String mKey, boolean mValue) {
        SharedPreferences mSharePrefs = context.getSharedPreferences(Pref_Name, 0);
        SharedPreferences.Editor editor;
        if (mSharePrefs != null) {
            editor = mSharePrefs.edit();
            editor.putBoolean(mKey, mValue);
            editor.commit();
        }
    }

    /**
     * 保存float类型的键值对到配置文件当中.
     */
    public void saveKey(String mKey, Float mValue) {
        SharedPreferences mSharePrefs = context.getSharedPreferences(Pref_Name, 0);
        SharedPreferences.Editor editor;
        if (mSharePrefs != null) {
            editor = mSharePrefs.edit();
            
            editor.putFloat(mKey, mValue);
            editor.commit();
        }
    }

    public void clear() {
        SharedPreferences mSharePrefs = context.getSharedPreferences(Pref_Name, 0);
        SharedPreferences.Editor editor;
        if (mSharePrefs != null) {
            editor = mSharePrefs.edit();
            editor.clear();
            editor.commit();
        }
    }

    /**
     * 
     * @ 功能   移除某个键
     * @param key
     * @ 作者  2014-7-13  / 黄赛赛
     */
    public void removeKey(String key) {
        SharedPreferences mSharePrefs = context.getSharedPreferences(Pref_Name, 0);
        SharedPreferences.Editor editor;
        if (mSharePrefs != null) {
            editor = mSharePrefs.edit();
            editor.remove(key);
            editor.commit();
        }
    }
    
	public void clear(String PrefName) {
		SharedPreferences mSharePrefs = context.getSharedPreferences(Pref_Name,
				0);
		SharedPreferences.Editor editor;
		if (mSharePrefs != null) {
			editor = mSharePrefs.edit();
			editor.remove(PrefName);
//			editor.clear();
			editor.commit();
		}
	}

}